var LOG = LOG || {};

/**
 * 日志管理- 问题处理日志
 */
LOG.process = Ext.extend(Ext.Panel,{
	closable:true
	,width:'100%'
	,initComponent:function(){
		this.tbar = this.buildBbar(this);
		this.items = this.buildGrid();
		LOG.userLogin.superclass.initComponent.call(this);
	}
	,buildBbar: function(mp) {
   		return [{
   				text: "删除日志",
   				handler:this.deleteLog,
    			iconCls: 'ico-task-delete'
	   		},'->',{
	   			xtype:'label',text:'处理时间：'
	   		},{
	   			xtype:'datefield',
	   			format:'Y-m-d',
	   			emptyText:'请选择处理开始时间',
	   			width:120,
	   			ref:'../processStartTime'
	   			,listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
	   		},'至',{
	   			xtype:'datefield',
	   			format:'Y-m-d',
	   			emptyText:'请选择处理结束时间',
	   			width:120,
	   			ref:'../processEndTime'
	   			,listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
	   		}
//	   		,{xtype:'spacer',width:10},{
//	   			xtype:'label',text:'问题单形成时间：'
//	   		},{
//	   			xtype:'datefield',
//	   			format:'Y-m-d',
//	   			emptyText:'请选择问题单形成时间',
//	   			width:120,
//	   			ref:'../endTime'
//	   			,listeners:{
//	                 'specialkey':function(f,e) {
//	                 	 if(e.getKey() == e.ENTER){
//	                 	 		var doSearch = mp.search ;
//	                 	 		mp.doSearch(doSearch);
//	                 	 }
//	                 }
//   				}
//	   		}
//	   		,{xtype:'spacer',width:10}
//	   		,{xtype:'label',text:'处理人：'}
//	   		,{
//   				 xtype:'textfield',
//   				 ref: '../searchText',
//   				 width:100,
//   				 value:'',
//	             emptyText:'请选择处理人',
//	             readOnly:true,
//	             listeners:{
//	                 'specialkey':function(f,e) {
//	                 	 if(e.getKey() == e.ENTER){
//	                 	 		var doSearch = mp.search ;
//	                 	 		mp.doSearch(doSearch);
//	                 	 }
//	                 }
//   				}
//   			}
	   	    ,{xtype:'spacer',width:10}
	   		,{xtype:'label',text:'稽核任务：'}
	   		,{
   				 xtype:'textfield',
   				 ref: '../taskId',
   				 width:100,
   				 value:'',
	             emptyText:'请输入任务编号',
	             listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
   			}
   			,{xtype:'spacer',width:10},{
   				text : "查询",
   				ref:"../search",
   				handler :this.doSearch,
   				iconCls: 'ico-dot-search'
   			 }] ;
   	}
	,buildGrid: function(){
//		var thisGrid = this;
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
					dataIndex: "ID",
	                sortable: false,
	                singleSelect:false,
	                width:20
				});
   	    var userLoginStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'logp_getProcessLog.action'
        	 ,fields:[ 'ID', 'PROCESS_KEY','TASK_ID' ,'PROC_INST_ID','PROCESS_TIME',
        		 'PROCESS_STATE' , 'ISSURE_ID'  ,'PROCESS_INFO'  ]
        	 ,root:'root'
        });
		return[{
			xtype:'grid'
			,viewConfig:{
				forceFit:true
			}
			,store:userLoginStore
			,sm:selectMode
			,bbar:[new Ext.PagingToolbar({
					pageSize : 25,
					store : userLoginStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							userLoginStore.recordStart = obj.start+1 ;
						    
						}
					}
				})]
			,columns:[selectMode,{
				        header:'流程定义KEY',
						sortable:true
						,dataIndex:"PROCESS_KEY"
					},{
				        header:'后台任务ID',
						sortable:true
						,dataIndex:"TASK_ID"
					},{
				        header:'流程实例ID',
						sortable:true
						,dataIndex:"PROC_INST_ID"
					},{
				        header:'处理时间',
						sortable:true
						,dataIndex:"PROCESS_TIME"
					},{
				        header:'处理状态',
						sortable:true
						,dataIndex:"PROCESS_STATE"
					}
					,{
						header:'问题单ID',
						sortable:true
						,dataIndex:"ISSURE_ID"
					}
					,{
						header:'处理结果',
						sortable:true,
						dataIndex:"PROCESS_INFO"
						,width:200
					}
			]
		}];
	}
	,doSearch: function(btn) {
		var processStartTime = btn.ownerCt.ownerCt.processStartTime.getValue();
		var processEndTime = btn.ownerCt.ownerCt.processEndTime.getValue();
   		var taskId = btn.ownerCt.ownerCt.taskId.getValue();
   		var store = btn.ownerCt.ownerCt.items.get(0).getStore();
   		if(processStartTime!=""){
   			processStartTime = processStartTime.format('Y-m-d');
   		}
   		if(processEndTime!=""){
   			processEndTime = processEndTime.format('Y-m-d');
   		}
   		
   		store.baseParams = {'processStartTime':processStartTime,'processEndTime':processEndTime, 'taskId':taskId} ;
   		store.load({
			params:{
				start:0,
				limit:25
			}
		});
	}
	,deleteLog:function(btn) {
   		var gp = btn.ownerCt.ownerCt.items.get(0) ;
   		var rows= gp.getSelectionModel().getSelections(); 
   		if(rows.length==0) {
   			 Ext.Msg.alert("提示", "请选择要操作的日志!");
   			 return ;
   		}
   		var ids = "";
   		for(var i=0 ;i<rows.length;i++) {
   			if(i==0) {
   				ids = rows[i].data['ID'] ;
   			}else{
   				ids = ids +","+rows[i].data['ID'] ;
   			}
   		}
   		Ext.MessageBox.confirm("确认", "确认要删除这些日志吗?", function(btn) {
					if (btn == "yes") {
						Ext.Ajax.request( {
							waitMsg : '正在执行  ',
							method : 'POST',
							params : {
								ids:ids 
							},
							url : 'logp_delProcessLog.action',
							scope : this,
							success : function(request) {
								var Result=Ext.decode(request.responseText);
								if(Result.success) {
									gp.getStore().reload();
								} else {
									 Ext.Msg.alert("错误",Result.info );
								}
							},
							failure : function() {
								Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
							}
						});
						
					}
				});
   	}
});